package com.ruoyi.module.app.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.page.PageFactory;
import com.ruoyi.common.page.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.ruoyi.module.app.mapper.ResourceCodeMapper;
import com.ruoyi.module.app.domain.ResourceCode;
import com.ruoyi.module.app.service.IResourceCodeService;

import java.util.List;

/**
 * 源码资源Service业务层处理
 *
 * @author mars
 * @date 2022-07-23
 */
@Slf4j
@Service
public class ResourceCodeServiceImpl extends ServiceImpl<ResourceCodeMapper, ResourceCode> implements IResourceCodeService {

    @Override
    public ResourceCode add(ResourceCode param) {
        super.save(param);
        return param;
    }

    @Override
    public boolean delete(Integer id) {
        return this.removeById(id);
    }

    /**
     * 批量删除
     *
     * @author value
     * @Date 2019-02-23
     */
    public boolean deleteBatch(List<Integer> ids) {
        return this.removeByIds(ids);
    }

    @Override
    public boolean update(ResourceCode param) {
        return this.updateById(param);
    }

    @Override
    public ResourceCode findById(Integer id) {
        return this.getById(id);
    }

    @Override
    @SuppressWarnings("all")
    public PageInfo findPageBySpec(ResourceCode param) {
        Page pageContext = getPageContext();
        LambdaQueryWrapper<ResourceCode> query = this.buildWrapper(param);
        log.info("查询参数：{}", JSONObject.toJSONString(query));
        IPage page = this.page(pageContext, query);
        log.info("查询返回结果：{}", JSONObject.toJSONString(page));
        return PageFactory.buildPageInfo(page);
    }

    /**
     * 构建查询条件
     *
     * @author mars
     * @Date 2022-07-23
     */
    private LambdaQueryWrapper<ResourceCode> buildWrapper(ResourceCode param) {
        LambdaQueryWrapper<ResourceCode> query = new LambdaQueryWrapper<>();
        if (param.getTechId() != null){
               query.eq(ResourceCode::getTechId ,param.getTechId());
        }
        if (param.getSubjectId() != null){
               query.eq(ResourceCode::getSubjectId ,param.getSubjectId());
        }
        if (StringUtils.isNotBlank(param.getTitle())){
               query.eq(ResourceCode::getTitle ,param.getTitle());
        }
        if (StringUtils.isNotBlank(param.getResourceIntro())){
               query.eq(ResourceCode::getResourceIntro ,param.getResourceIntro());
        }
        if (param.getPrice() != null){
               query.eq(ResourceCode::getPrice ,param.getPrice());
        }
        if (param.getOriginPrice() != null){
               query.eq(ResourceCode::getOriginPrice ,param.getOriginPrice());
        }
        if (param.getLessonNum() != null){
               query.eq(ResourceCode::getLessonNum ,param.getLessonNum());
        }
        if (param.getResourceType() != null){
               query.eq(ResourceCode::getResourceType ,param.getResourceType());
        }
        if (StringUtils.isNotBlank(param.getAttachmentInfo())){
               query.eq(ResourceCode::getAttachmentInfo ,param.getAttachmentInfo());
        }
        if (StringUtils.isNotBlank(param.getCover())){
               query.eq(ResourceCode::getCover ,param.getCover());
        }
        if (StringUtils.isNotBlank(param.getDescription())){
               query.eq(ResourceCode::getDescription ,param.getDescription());
        }
        if (param.getSort() != null){
               query.eq(ResourceCode::getSort ,param.getSort());
        }
        if (param.getEnable() != null){
               query.eq(ResourceCode::getEnable ,param.getEnable());
        }
        if (param.getStatus() != null){
               query.eq(ResourceCode::getStatus ,param.getStatus());
        }
        if (StringUtils.isNotBlank(param.getRemarks())){
               query.eq(ResourceCode::getRemarks ,param.getRemarks());
        }
        return query;
    }


    private Page<Object> getPageContext() {
        return PageFactory.buildPage();
    }
}
